package com.e3.basdoc.region.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.region.model.RegionFields;
import com.e3.basdoc.region.model.RegionModel;
import com.e3.template.tgf.TGFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

/**
 * Description: <br>
 * 
 * @author: <a href="mailto:senton1101@126.com">TianXiangdong</a> 2010-8-6
 */
public class RegionHelper extends TGFHelper<RegionModel> {

	private String[] notNullFieldNames = {RegionModel.CODE,RegionModel.NAME};
	
	public static RegionHelper INSTANCE = new RegionHelper();
	
	public Field<?> createFormField(String name) {
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNull = isNotNullField(name);
		if (name.equals(RegionFields.CODE)) {
			formField = MyFormFieldFactory.createStringField(name, dispName,10,isNull);
		}
		else if( name.equals(RegionFields.NAME)){
			formField = MyFormFieldFactory.createStringField(name, dispName,20,isNull);
		}
		else if(name.equals(RegionFields.ENGLISH_NAME)){
			formField = MyFormFieldFactory.createStringField(name, dispName,0,isNull);
		}
		else if( name.equals(RegionFields.ZIPCODE)){
			formField = MyFormFieldFactory.createStringField(name, dispName,10,isNull);
		}

		else if (name.equals(RegionFields.PARENT_REGION)) {
			formField = MyFormFieldFactory.createRefField(name, dispName, new RegionRefDialog(),isNull);
		}
		else if (name.equals(RegionFields.ID)) {
			formField = MyFormFieldFactory.createIdField(name);
			formField.hide();
		}
		return formField;
	}

	/**
	 * 设置地区之间的父子关系。
	 * 
	 * @param pRegion
	 * @param children
	 */
	public void setRegionRelations(RegionModel pRegion, List<RegionModel> children) {
		for (RegionModel regionModel : children) {
			regionModel.setParentModel(pRegion);
		}
	}

	public List<ColumnConfig> getGridColumnConfigs() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();
		configs.add(numberer);

		ColumnConfig column = new ColumnConfig();
		String name = RegionModel.CODE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = RegionModel.NAME;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = RegionModel.PARENT_REGION;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = RegionModel.ENGLISH_NAME;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = RegionModel.ZIPCODE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		return configs;
	}

	public String getFieldDisplayName(String code) {
		return RegionFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String getCodeFieldName() {
		return RegionModel.CODE;
	}

	@Override
	public RegionModel createModelInstance() {
		return new RegionModel();
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFieldNames;
	}
}
